local awful = require("awful")
local gears = require("gears")
local wibox = require("wibox")
local naughty = require("naughty")
local dpi   = require("beautiful.xresources").apply_dpi

local apps = require("config.apps")

local cmus_widget        = require('widgets.cmus-widget.cmus')
local volume_widget      = require('widgets.volume-widget.volume')
local logout_menu_widget = require("widgets.logout-menu-widget.logout-menu")
local net_speed_widget   = require("widgets.net-speed-widget.net-speed")
local ram_widget         = require("widgets.ram-widget.ram-widget")
local calendar_widget    = require("widgets.calendar-widget.calendar")
local cpu_widget         = require("widgets.cpu-widget.cpu-widget")
local docker_widget      = require("widgets.docker-widget.docker")
local battery_widget     = require("widgets.battery-widget.battery")
local cmus_widget        = require('widgets.cmus-widget.cmus')

local qemu_menu_widget     = require("widgets.qemu-menu-widget.qemu-menu")
local nftables_menu_widget = require("widgets.nftables-menu-widget.nftables-menu")
local wifi_menu_widget     = require("widgets.wifi-menu-widget.wifi-menu")

local ICON_DIR = '/home/q/.config/awesome/icons/'

-- Create a textclock widget
mytextclock = wibox.widget.textclock()
local cw = calendar_widget({
    theme = 'dark',
    placement = 'top_right',
    start_sunday = true,
    radius = 8,
-- with customized next/previous (see table above)
    previous_month_button = 1,
    next_month_button = 3,
})
mytextclock:connect_signal("button::press",
    function(_, _, _, button)
        if button == 1 then cw.toggle() end
    end)

-- Create a wibox for each screen and add it
local taglist_buttons = gears.table.join(
                    awful.button({ }, 1, function(t) t:view_only() end),
                    awful.button({ modkey }, 1, function(t)
                                              if client.focus then
                                                  client.focus:move_to_tag(t)
                                              end
                                          end),
                    awful.button({ }, 3, awful.tag.viewtoggle),
                    awful.button({ modkey }, 3, function(t)
                                              if client.focus then
                                                  client.focus:toggle_tag(t)
                                              end
                                          end),
                    awful.button({ }, 4, function(t) awful.tag.viewnext(t.screen) end),
                    awful.button({ }, 5, function(t) awful.tag.viewprev(t.screen) end)
                )

local tasklist_buttons = gears.table.join(
                     awful.button({ }, 1, function (c)
                                              if c == client.focus then
                                                  c.minimized = true
                                              else
                                                  c:emit_signal(
                                                      "request::activate",
                                                      "tasklist",
                                                      {raise = true}
                                                  )
                                              end
                                          end),
                     awful.button({ }, 3, function()
                                              awful.menu.client_list({ theme = { width = 250 } })
                                          end),
                     awful.button({ }, 4, function ()
                                              awful.client.focus.byidx(1)
                                          end),
                     awful.button({ }, 5, function ()
                                              awful.client.focus.byidx(-1)
                                          end))

awful.screen.connect_for_each_screen(function(s)
    -- Create a promptbox for each screen
    s.mypromptbox = awful.widget.prompt()
    -- Create an imagebox widget which will contain an icon indicating which layout we're using.
    -- We need one layoutbox per screen.
    s.mylayoutbox = awful.widget.layoutbox(s)
    s.mylayoutbox:buttons(gears.table.join(
                           awful.button({ }, 1, function () awful.layout.inc( 1) end),
                           awful.button({ }, 3, function () awful.layout.inc(-1) end),
                           awful.button({ }, 4, function () awful.layout.inc( 1) end),
                           awful.button({ }, 5, function () awful.layout.inc(-1) end)))
    -- Create a taglist widget
    s.mytaglist = awful.widget.taglist {
        screen  = s,
        filter  = awful.widget.taglist.filter.all,
        buttons = taglist_buttons
    } -- Create a tasklist widget
    s.mytasklist = awful.widget.tasklist {
        screen  = s,
        filter  = awful.widget.tasklist.filter.currenttags,
        buttons = tasklist_buttons
    }

    -- Create the wibox
    s.mywibox = awful.wibar({ position = "top", screen = s })

    mylauncher = awful.widget.launcher({ image = ICON_DIR .. "menu.png",
                                         menu = mymainmenu })

    -- Add widgets to the wibox
    s.mywibox:setup {
        layout = wibox.layout.align.horizontal,
        { -- Left widgets
            layout = wibox.layout.fixed.horizontal,
            --mylauncher,
            s.mytaglist,
            s.mypromptbox,
        },
	    s.mytasklist, -- Middle widget
        { -- Right widgets
            layout = wibox.layout.fixed.horizontal,
            mykeyboardlayout,
	    --email_icon,
	    --email_widget,
	    cmus_widget(),
	    net_speed_widget{
              interface = 'eth0',
              --interface = 'docker0',
	    },
	    cpu_widget({
              width = 70,
              step_width = 3,
              step_spacing = 1,
	      color = '#434c5e'
	    }),
	    ram_widget(),
	    docker_widget{
              number_of_containers = 5
            },
	    qemu_menu_widget{
              font = 'Nerd 9',
              onvm1 = function() awful.spawn.with_shell('/home/q/VMS/scripts/GATEWAY_VPN.sh') naughty.notify({ title = "QEMU", text = "GATEWAY_VPN machine started", timeout = 2 }) end,
              onvm2 = function() awful.spawn.with_shell('/home/q/VMS/scripts/PRIVITE_WEB.sh') naughty.notify({ title = "QEMU", text = "PRIVITE_WEB machine started", timeout = 2 }) end,
              onvm3 = function() awful.spawn.with_shell('/home/q/VMS/scripts/PRIVITE_SOCIALS.sh') naughty.notify({ title = "QEMU", text = "PRIVITE_SOCIALS machine started", timeout = 2 }) end,
              onvm4 = function() awful.spawn.with_shell('/home/q/VMS/scripts/PRIVITE_WALLET.sh') naughty.notify({ title = "QEMU", text = "PRIVITE_WALLET machine started", timeout = 2 }) end,
              onvm6 = function() awful.spawn.with_shell('/home/q/VMS/scripts/YGGDRASIL.sh') naughty.notify({ title = "QEMU", text = "YGGDRASIL machine started", timeout = 2 })end,
              onvm6 = function() awful.spawn.with_shell('/home/q/VMS/scripts/FREENET.sh') naughty.notify({ title = "QEMU", text = "FREENET machine started", timeout = 2 }) end,
              onvm7 = function() awful.spawn.with_shell('/home/q/VMS/scripts/I2P.sh') naughty.notify({ title = "QEMU", text = "I2P machine started", timeout = 2 }) end,
	      onvm8 = function() awful.spawn.with_shell('/home/q/VMS/scripts/TOR.sh') naughty.notify({ title = "QEMU", text = "TOR machine started", timeout = 2 }) end,
	      onvm9 = function() awful.spawn.with_shell('/home/q/VMS/scripts/ZERONET.sh') naughty.notify({ title = "QEMU", text = "ZERONET machine started", timeout = 2 }) end,
              onvm10 = function() awful.spawn.with_shell('/home/q/VMS/scripts/PUBLIC.sh') naughty.notify({ title = "QEMU", text = "PUBLIC machine started", timeout = 2 }) end,
              onvm11 = function() awful.spawn.with_shell('/home/q/VMS/scripts/PUBLIC_SERVERS.sh') naughty.notify({ title = "QEMU", text = "PUBLIC_SERVERS machine started", timeout = 2 }) end
     	    },
	    nftables_menu_widget{
              font = 'Nerd 9',
              onn1 = function() awful.spawn.with_shell('sudo /home/q/.config/awesome/scripts/nftables/BLOCK_ALL.sh') naughty.notify({ title = "Firewall [Nftables]", text = "Activated profile [BLOCK ALL] ", timeout = 2 }) end,
              onn2 = function() awful.spawn.with_shell('sudo /home/q/.config/awesome/scripts/nftables/BLOCK_LOCAL_DOCKER_ALL.sh') naughty.notify({ title = "Firewall [Nftables]", text = "Activated profile [BLOCK_LOCAL_DOCKER_ALL] ", timeout = 2 }) end,
              onn3 = function() awful.spawn.with_shell('sudo /home/q/.config/awesome/scripts/nftables/ACCEPT_v4.sh') naughty.notify({ title = "Firewall [Nftables]", text = "Activated profile [ACCEPT_v4] ", timeout = 2 }) end,
              onn4 = function() awful.spawn.with_shell('sudo /home/q/.config/awesome/scripts/nftables/ACCEPT_v4_v6.sh') naughty.notify({ title = "Firewall [Nftables]", text = "Activated profile [ACCEPT_v4_v6] ", timeout = 2 }) end,
              onn5 = function() awful.spawn.with_shell('sudo /home/q/.config/awesome/scripts/nftables/ACCEPT_DOCKER_v4.sh') naughty.notify({ title = "Firewall [Nftables]", text = "Activated profile [ACCEPT_DOCKER_v4] ", timeout = 2 }) end,
              onn6 = function() awful.spawn.with_shell('sudo /home/q/.config/awesome/scripts/nftables/ACCEPT_DOCKER_v4_v6.sh') naughty.notify({ title = "Firewall [Nftables]", text = "Activated profile [ACCEPT_DOCKER_v4_v6] ", timeout = 2 }) end,
              onn7 = function() awful.spawn.with_shell('sudo /home/q/.config/awesome/scripts/nftables/ACCEPT_DOCKER_DISCORD_v4.sh') naughty.notify({ title = "Firewall [Nftables]", text = "Activated profile [ACCEPT_DOCKER_DISCORD_v4] ", timeout = 2 }) end,
              onn8 = function() awful.spawn.with_shell('sudo /home/q/.config/awesome/scripts/nftables/FLUSH.sh') naughty.notify({ title = "Firewall [Nftables]", text = "Run flush ruleset", timeout = 2 }) end,
              onn9 = function() awful.spawn(apps.terminal..' -e sudo /home/q/.config/awesome/scripts/nftables/CHECK.sh') naughty.notify({ title = "Firewall [Nftables]", text = "Run list ruleset", timeout = 2 }) end,
              onn10 = function() awful.spawn(apps.terminal..' -e sudo /home/q/.config/awesome/scripts/nftables/MONITOR.sh') naughty.notify({ title = "Firewall [Nftables]", text = "Start monitoring", timeout = 2 }) end
	    },
	    wifi_menu_widget{
              font = 'Nerd 9',
              onscript = function() awful.spawn(apps.terminal..' -e sudo /home/q/.config/awesome/scripts/wifi/wpa_supplicant.sh') end,
              onedit = function() awful.spawn(apps.terminal..' -e sudo /home/q/.config/awesome/scripts/wifi/edit.sh') end
            },
	    volume_widget(),
	    battery_widget{
	    	margin_left = 5
            },
            mytextclock,
            wibox.widget.systray(),
	    logout_menu_widget{
              font = 'Nerd 9',
              onlock = function() awful.spawn.with_shell('i3lock-fancy') end,
	      onlogout = function () awesome.quit() end,
              onreboot = function() awful.spawn.with_shell("sudo reboot") end,
	      onsuspend = function() awful.spawn.with_shell("loginctl suspend") end,
              onpoweroff = function() awful.spawn.with_shell("sudo poweroff") end
            },
            s.mylayoutbox
        },
    }

end)
